package de.duester.genealogy;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class LoadFamilyTreeActivity extends Activity implements OnClickListener {
	private Spinner spDBNames;
	private Button btnOk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_load_family_tree);
		init();
	}

	private void init() {
		spDBNames = (Spinner) findViewById(R.id.spDBNames);
		String[] dbNamesArray = databaseList();
		List<String> dbNames = new ArrayList<String>();
		for (String s : dbNamesArray)
			if (s.endsWith(".db"))
				dbNames.add(s.substring(0, s.length() - 3));
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, dbNames);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spDBNames.setAdapter(adapter);

		btnOk = (Button) findViewById(R.id.btnLoadFamilyTreeOk);
		btnOk.setOnClickListener(this);

		if (dbNames.size() == 0)
			btnOk.setEnabled(false);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		intent.putExtra("dbName", spDBNames.getSelectedItem().toString());
		setResult(RESULT_OK, intent);
		finish();
	}

}
